<?php
/**
 * Udp服务
 *
 * @author		黑冰 <954650386@qq.com>
 */

namespace waf\server;

use \waf\Waf;
use \waf\log\WLog;
use \waf\server\BaseServer;
use \Yaf;

class UdpServer extends BaseServer
{
    public function init()
    {
        WLog::logSystem("in", __FILE__, __LINE__, __FUNCTION__);
        $this->strServerType = 'udp';
        parent::init();
        WLog::logSystem("out", __FILE__, __LINE__, __FUNCTION__);
    }

    public function onPacket($objSServer, $data, $arrAddr)
    {
        //WLog::logInfo("onPacket", __FILE__, __LINE__, __FUNCTION__);
        $ret = $this->route($data);
        if($ret['r'] === 0 && $ret['data'])
        {
            try {
                ob_start();

                $requestObj = new \Yaf_Request_Http($ret['data']['url']);
                $this->setParams($requestObj, $ret['data']);
                $requestObj->setParam('arrAddr', $arrAddr);
                $this->objYaf->getDispatcher()->dispatch($requestObj);

                $result = ob_get_contents();
                ob_end_clean();
                $objSServer->sendto($arrAddr['address'], $arrAddr['port'], $result);
            } catch (\Throwable $e) {
                WLog::logError("yaf_exception:".WLog::toJson(print_r($e, true)), __FILE__, __LINE__, __FUNCTION__);
            }
        }else{
            WLog::logError("route data exception. receive_data = ".$data, __FILE__, __LINE__, __FUNCTION__);
        }
    }
}
?>